package be.jabapage.snooker.service.competition.impl;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import org.apache.commons.lang.Validate;

import be.jabapage.snooker.exception.EntityNotFoundException;
import be.jabapage.snooker.jdo.competition.CompetitionGameFrame;
import be.jabapage.snooker.service.competition.api.ICompetitionGameFrameService;


public class AppEngineCompetitionGameFrameService implements ICompetitionGameFrameService, Serializable {

	private static final long serialVersionUID = -3227111043869411398L;
	private final PersistenceManagerFactory pmf;

	public AppEngineCompetitionGameFrameService(final PersistenceManagerFactory pmf) {
		Validate.notNull(pmf,"The persistence Manager factory cannot be null");
		this.pmf = pmf;
	}

	/**
	 * {@inheritDoc}
	 */
	public void store(final CompetitionGameFrame competitionGameFrame) {
		Validate.notNull(competitionGameFrame);
		PersistenceManager pm = pmf.getPersistenceManager();
		try {
			pm.makePersistent(competitionGameFrame);
		} finally {
			pm.close();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public CompetitionGameFrame retrieve(final Long key) throws EntityNotFoundException {
		Validate.notNull(key, "A key should be provided.");
		PersistenceManager pm = pmf.getPersistenceManager();
		try {
			CompetitionGameFrame objectById = pm.getObjectById(CompetitionGameFrame.class, key);
			if (objectById == null) {
				throw new EntityNotFoundException();
			}
			return objectById;
		} finally {
			pm.close();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public Set<CompetitionGameFrame> retrieveForCompetitionGamePlayer(final Long competitionGamePlayerId, final boolean home) {
		Validate.notNull(competitionGamePlayerId);
		PersistenceManager pm = pmf.getPersistenceManager();
		Set<CompetitionGameFrame> resultSet = new HashSet<CompetitionGameFrame>();
		Query query = pm.newQuery(CompetitionGameFrame.class);
		try {
			if (home) {
				query.setFilter("playerHome == playerParam");
			} else {
				query.setFilter("playerAway == playerParam");
			}
			query.declareParameters("Long playerParam");
			@SuppressWarnings("unchecked")
			List<CompetitionGameFrame> result = (List<CompetitionGameFrame>) query.execute(competitionGamePlayerId);
			if (result != null) {
				resultSet.addAll(result);
			}
		} finally {
			query.closeAll();
			pm.close();
		}
		return resultSet;
	}

	public void delete(final Long key) throws EntityNotFoundException {
		Validate.notNull(key, "We cannot delete an instance withour a key.");
		PersistenceManager pm = pmf.getPersistenceManager();
		try {
			CompetitionGameFrame dataInstance = pm.getObjectById(CompetitionGameFrame.class, key);
			if (dataInstance == null) {
				throw new EntityNotFoundException();
			}

			pm.deletePersistent(dataInstance);
		} finally {
			pm.close();
		}
	}


}
